package com.group18.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group18.service.AccountUserService;


/**
 * Servlet implementation class DoDeleteAccount
 */
@WebServlet("/DoDeleteAccount")
public class DoDeleteAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoDeleteAccount() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		//删除收支
		int id = Integer.parseInt(request.getParameter("id"));
		AccountUserService aus = new AccountUserService();
		boolean bl = aus.deleteByID(id);
		String msg = "";
		
		if(bl) {
			msg = "删除成功";
		}else {
			msg = "删除失败";
		}
		
//		使用会话返回
//		HttpSession session = request.getSession();
//		session.setAttribute("msg", msg);
		//保证在执行过搜索的页面上进行删除操作后，能回到搜索后的页面而不是主页面
		request.setAttribute("msg", msg);
		String text = request.getParameter("text");
		if(text != null && !text.equals("")) {
			request.setAttribute("text", text);
			request.getRequestDispatcher("/DoSearchAccount").forward(request, response);
		}else {
			request.getRequestDispatcher("/ToIndex").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
